﻿#region [ Modification History ]
/*
 * 06/06/2013 Denis Kholine
 *  Add license and description.
 */
#endregion

#region  [ UIUC NCSA Open Source License ]
/*****************************************************************************\
 *             University of Illinois/NCSA Open Source License
 *
 * 
 *      Copyright © 2012,2013, The Board of Trustees of the University of Illinois. All rights reserved.
 *
 *      Developed by: Rakesh Bobba, Derek Dagit, Erich Heine, Denis Kholine,
 *                    Klara Nahrstedt, David Rodgers,   William H. Sanders, Tim Yardley
 *
 *      Information Trust Institute/Coordinated Science Laboratory
 * 
 *      University of Illinois
 *      
 *      The Board of Trustees of the University of Illinois
 *      109 Coble Hall
 *      801 South Wright Street
 *      Champaign, IL 61820-6242
 *
 *      http://www.iti.illinois.edu / http://csl.illinois.edu
 * 
 *      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 *      associated documentation files (the “Software”), to deal with the Software without restriction, 
 *      including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *      and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 *      subject to the following conditions:
 *
 *      Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *      following disclaimers.
 *      
 *      Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 *      the following disclaimers in the documentation and/or other materials provided with the distribution.
 *
 *      Neither the names of Information Trust institute, Coordinated Science Laboratory, University of Illinois,
 *      The Board of Trustees of the University of Illinois nor the names of its contributors may be used to 
 *      endorse or promote products derived from this Software without specific prior written permission.
 *      
 *      THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 *      LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN
 *      NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *      WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 *      SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 *      
 \*****************************************************************************/
#endregion

#region [ Using ]
using System;
using System.Data;
using System.Linq;
using System.Diagnostics;
using SIEGate.Core;
using SIEGate.Core.Logging;
using SIEGate.Core.Fakes;
using SIEGate.Core.Notifications;
using SIEGate.Core.MessageBusses;
using SIEGate.Core.Configurator;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GSF;
using GSF.TimeSeries.Transport;
#endregion

namespace SIEGateCoreTest.Logging
{
    [TestClass]
    public class EventViewerLogTest
    {
        #region [ Methods ]
        [TestMethod]
        [TestCategory("EventViewerLogger")]
        public void TestEventViewerLogger()
        {
            string source = "Application";
            EventViewerLogger logger = new EventViewerLogger(source);
            string message = "Test message " + Guid.NewGuid().ToString();
            DateTime logTime = DateTime.Now;
            logger.Log(LogLevel.Info, logTime, "Unit Test", "Test", message);
            EventLogEntryCollection events = EventLog.GetEventLogs().First(x => x.Log.Equals("Application")).Entries;
            bool passing = false;
            foreach (EventLogEntry e in events)
            {
                if (e.Source.Equals(source))
                {
                    if (e.TimeGenerated.ToString().Equals(logTime.ToString())
                        || e.TimeWritten.ToString().Equals(logTime.ToString()))
                    {
                        passing = true;
                        break;
                    }
                }
            }
            Assert.IsTrue(passing, "Did not find randomly-generated message at correct time.");
        }

        /// <summary>
        /// A Log test
        /// </summary>
        [TestMethod]
        [TestCategory("EventViewerLogger")]
        public void LogTest()
        {
            string source = "SIEGateUnitTesting";
            string logname ="SIEGateLog";
            string message = "Log Testing";
            EventSourceCreationData eventsourcedata = new EventSourceCreationData(source,logname);
            try
            {
                EventLog.CreateEventSource(eventsourcedata);
            }
            catch
            {
                EventLog.Delete(logname);
                EventLog.CreateEventSource(eventsourcedata);
            }
            
            EventLog log = new EventLog(logname);
            EventViewerLogger logger = new EventViewerLogger(source);
            DateTime date = new DateTime();
            bool actual = false;
            bool expected = true;
            foreach (LogLevel levelitem in Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>())
            {
                logger.Log(levelitem, date, source, logname, message);
                actual = (log.Entries.Count==1);
                Assert.AreEqual(expected,actual);
                // clean up log
                actual = false;
                log.Clear();
            }

            //delete log
            log.Dispose();
            EventLog.Delete(logname); 
            
        }


        /// <summary>
        /// A LogException test
        /// </summary>
        [TestMethod]
        [TestCategory("EventViewerLogger")]
        public void LogExceptionTest()
        {
            string source = "SIEGateUnitTesting";
            string logname = "SIEGateLog";
            EventSourceCreationData eventsourcedata = new EventSourceCreationData(source, logname);
            try
            {
                EventLog.CreateEventSource(eventsourcedata);
            }
            catch
            {
                EventLog.Delete(logname);
                EventLog.CreateEventSource(eventsourcedata);
            }

            EventLog log = new EventLog(logname);
            EventViewerLogger logger = new EventViewerLogger(source);
            DateTime date = new DateTime();
            Exception ex = new Exception();
            bool actual = false;
            bool expected = true; 
            foreach (LogLevel levelitem in Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>())
            {
                logger.LogException(levelitem, date, source, logname,ex); 
                actual = (log.Entries.Count == 1);
                Assert.AreEqual(expected, actual);
                // clean up log
                actual = false;
                log.Clear();
            }

            //delete log
            log.Dispose();
            EventLog.Delete(logname);

        }
        #endregion
    }
}
